<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>图书管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加图书</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-form :model="addForm" :rules="addRules" ref="addFormRef" label-position="right">
        <el-form-item label="图书名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input  v-model="addForm.author"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">添加</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      addForm: {
        name: '',
        author: ''
      },
      addRules: {
        name: [
          { required: true, message: '请输入图书名称', trigger: 'blur' }
        ],
        author: [
          { required: true, message: '请输入作者姓名', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('book/save', this.addForm)
        console.log(res)

        if (res !== 'success') return this.$message.error('添加图书失败！')
        this.$router.push('/book')
        return this.$message.success('添加图书成功！')
      })
    }
  }
}
</script>

<style>
</style>
